package cn.imvc.app.models;



import java.util.Date;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.imvc.app.domains.AppResult;
import cn.imvc.app.domains.FundFlowType;
import cn.imvc.app.domains.OrderState;
import cn.imvc.app.domains.OrderType;
import cn.imvc.app.entities.DealerApply;
import cn.imvc.app.entities.GoodsOrder;
import cn.imvc.app.entities.Member;
import cn.imvc.app.entities.Orders;
import cn.imvc.app.entities.Recharge;
import cn.imvc.app.entities.StorageOrder;
import cn.imvc.ipay.IPayChannel;

@Service
public class PayModelImpl implements PayModel{
	
	@Autowired
	DealerApplyModel dealerApplyModel;
	
	@Autowired
	AccountModel accountModel;
	
	@Autowired
	MemberModel memberModel;
	
	@Autowired
	OrderModel orderModel;
	
	@Autowired
	RechargeModel rechargeModel;
	
	@Autowired
	FundFlowModel fundFlowModel;
	
	@Autowired
	StorageOrderModel storageOrderModel;
	
	@Autowired
	GoodsOrderModel goodsOrderModel;
	
	public Integer setOrderPayment(String code,Byte payChannel){
		
		String type = code.substring(0, 2);
		Integer money=0;
		if (OrderType.Upgrade.getValue().equals(type)) {			
			DealerApply dealerApply=dealerApplyModel.findByCode(code);
			if(dealerApply==null||dealerApply.getState()!=OrderState.Create.getValue()&&dealerApply.getState()!=OrderState.Payment.getValue()){
				return 0;
			}
			dealerApply.setState(OrderState.Payment.getValue());
			dealerApply.setUpdatedAt(new Date());
			dealerApply.setPayChannel(payChannel);
			dealerApplyModel.save(dealerApply);
			money=dealerApply.getJoinMoney();
		} else if (OrderType.Order.getValue().equals(type)) {
			Orders order=orderModel.findByCode(code);
			if(order==null||order.getState()!=OrderState.Create.getValue()&&order.getState()!=OrderState.Payment.getValue()){
				return 0;
			}
			order.setState(OrderState.Payment.getValue());
			order.setUpdatedAt(new Date());
			order.setPayChannel(payChannel);
			orderModel.save(order);
			Set<StorageOrder> storageOrders = order.getStorageOrder();
			for (StorageOrder storageOrder : storageOrders) {
				storageOrder.setState(OrderState.Payment.getValue());
				storageOrder.setUpdatedAt(new Date());
				storageOrderModel.save(storageOrder);
				Set<GoodsOrder> goodsOrders = storageOrder.getGoodsOrder();
				for (GoodsOrder goodsOrder : goodsOrders) {
					goodsOrder.setState(OrderState.Payment.getValue());
					goodsOrder.setUpdatedAt(new Date());
					goodsOrderModel.save(goodsOrder);
				}
			}
			money=order.getResultAmount();
		} else if (OrderType.Recharge.getValue().equals(type)) {
			Recharge recharge=rechargeModel.findByCode(code);
			if(recharge==null||recharge.getState()!=OrderState.Create.getValue()&&recharge.getState()!=OrderState.Payment.getValue()){
				return 0;
			}
			recharge.setState(OrderState.Payment.getValue());
			recharge.setUpdatedAt(new Date());
			recharge.setPayChannel(payChannel);
			rechargeModel.save(recharge);
			money=recharge.getMoney();			
		}
		return money;
	}
	
	public AppResult balancePay(String code,Member member){
		AppResult result=new AppResult();		
		//更改支付状态
		Integer money=setOrderPayment(code,(byte)IPayChannel.pursePay.getValue());
		if(money==0){
			result.setCode("0");
			result.setMsg("支付失败");
			return result;
		}
		if(member.getAccount().getPurse()==null||money>member.getAccount().getPurse()){
			result.setCode("0");
			result.setMsg("余额不足");
			return result;
		}
		String type = code.substring(0, 2);
		if (OrderType.Upgrade.getValue().equals(type)) {			
			dealerApplyModel.finishPay(code,"",new Date());
		} else if (OrderType.Order.getValue().equals(type)) {
			orderModel.finishPay(code,"",new Date());
		} else if (OrderType.Recharge.getValue().equals(type)) {
			rechargeModel.finishPay(code,"",new Date());
		}
		accountModel.retrySaveAccount(member.getAccount().getId(), -money,0,0);
		result.setCode("1");
		result.setMsg("支付成功");
		
		return result;
	}

	
	public AppResult moneyPay(Map<String ,Object> map){
		AppResult result=new AppResult();
		String code=map.get("code").toString();
		String type = code.substring(0, 2);
		Member member=null;
		int money=0;
		Byte state=0;
		Byte fundFlowType=0;
		if (OrderType.Upgrade.getValue().equals(type)) {			
			DealerApply dealerApply=dealerApplyModel.findByCode(code);
			money=dealerApply.getJoinMoney();
			state=dealerApply.getState();
			member=dealerApply.getMember();
			fundFlowType=FundFlowType.UpgradeRecharge.getValue();
		} else if (OrderType.Order.getValue().equals(type)) {
			Orders order=orderModel.findByCode(code);
			money=order.getResultAmount();
			state=order.getState();
			member=order.getMember();
			fundFlowType=FundFlowType.BuyRecharge.getValue();
		} else if (OrderType.Recharge.getValue().equals(type)) {			
			Recharge recharge=rechargeModel.findByCode(code);
			money=recharge.getMoney();	
			state=recharge.getState();
			member=memberModel.get(recharge.getMemberId());		
			fundFlowType=FundFlowType.Recharge.getValue();
		}
		if(state!=OrderState.Payment.getValue()){
			result.setCode("0");
			result.setMsg("已处理过");
			return result;
		}
		if(money!=Integer.parseInt(map.get("money").toString())){
			result.setCode("0");
			result.setMsg("金额不正确");
			return result;
		}
		fundFlowModel.addFundFlow(member, money, code,fundFlowType,member);
		
		if (OrderType.Upgrade.getValue().equals(type)) {			
			dealerApplyModel.finishPay(code,map.get("payCode").toString(),(Date)map.get("time"));
		} else if (OrderType.Order.getValue().equals(type)) {
			orderModel.finishPay(code,map.get("payCode").toString(),(Date)map.get("time"));
		} else if (OrderType.Recharge.getValue().equals(type)) {
			rechargeModel.finishPay(code,map.get("payCode").toString(),(Date)map.get("time"));	
		}
		result.setCode("1");
		result.setMsg("成功");
		return result;
	}
}
